Variables

resources used - http://learningtensorflow.com/lesson2/

Section 1 - a simple representation

A simple representation of variables and constants in a tf graph


In [1]:
import tensorflow as tf

x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')

model = tf.global_variables_initializer()

Running the graph in a tf session


In [2]:
with tf.Session() as session:
    session.run(model)
    print(session.run(y))


40

Section 2 - moving average


In [3]:
import tensorflow as tf

x = tf.Variable(0, name='x')

model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    for i in range(5):
        x = x + 1
        avg = x/(i+1)
        print('x = ',session.run(x))
        print('moving avg = ',session.run(avg))


x =  1
moving avg =  1.0
x =  2
moving avg =  1.0
x =  3
moving avg =  1.0
x =  4
moving avg =  1.0
x =  5
moving avg =  1.0